package kerberos;

import java.util.HashMap;
import java.util.Map;

import javax.security.auth.Subject;

import com.sun.security.auth.module.Krb5LoginModule;

public class Krb {
	private void loginImpl(final String propertiesFileName) throws Exception {
		System.out.println("NB: system property to specify the krb5 config: [java.security.krb5.conf]");
		System.setProperty("java.security.krb5.conf", "E:\\hadoop\\jaas\\src\\main\\resources\\conf\\krb5.conf");

		System.out.println(System.getProperty("java.version"));

		System.setProperty("sun.security.krb5.debug", "false");

		final Subject subject = new Subject();

		final Krb5LoginModule krb5LoginModule = new Krb5LoginModule();
		final Map<String, String> optionMap = new HashMap<String, String>();

		// optionMap.put("ticketCache", "/tmp/krb5cc_1000");
		optionMap.put("keyTab", "E:\\hadoop\\jaas\\src\\main\\resources\\conf\\test.keytab");
		optionMap.put("principal", "test@MYSPRING.COM"); // default realm

		optionMap.put("doNotPrompt", "true");
		optionMap.put("refreshKrb5Config", "true");
		optionMap.put("useTicketCache", "true");
		optionMap.put("renewTGT", "true");
		optionMap.put("useKeyTab", "true");
		optionMap.put("storeKey", "true");
		optionMap.put("isInitiator", "true");

		optionMap.put("debug", "true"); // switch on debug of the Java
										// implementation

		krb5LoginModule.initialize(subject, null, new HashMap<String, String>(), optionMap);

		boolean loginOk = krb5LoginModule.login();
		System.out.println("======= login:  " + loginOk);

		boolean commitOk = krb5LoginModule.commit();
		System.out.println("======= commit: " + commitOk);
		System.out.println("======= Subject: " + subject);
	}

	public static void main(String[] args) throws Exception {
		System.out
				.println("A property file with the login context can be specified as the 1st and the only paramater.");
		final Krb krb = new Krb();
		krb.loginImpl(null);
	}
}
